<template>
  <div>
    <el-dialog
      :title="title + '话题'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="1000px"
      @close="resetForm('form')"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="80px"
        :validate-on-rule-change="false"
      >
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model.trim="form.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类" prop="classifyId">
              <ClassifySelect
                :defaultValue="form.classifyId"
                @emit="(v) => (form.classifyId = v)"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否发布" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio label="0">否</el-radio>
                <el-radio label="1">是</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="contents">
              <UEditor
                ref="ueditor"
                :ueHeight="360"
                :ueWidth="875"
                :defaultMsg="form.contents"
                :clear="dialogVisible"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {
    ClassifySelect: () => import('../../components/ClassifySelect.vue'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        title: '', //标题
        status: '0', //状态;0-未发布，1-已发布
        selection: '0', //精选;0-非精选（默认），1-精选
        auditStatus: '1', //审核状态;1-待审核，2-审核通过，3-不通过
        classifyId: '', //分类id
        contents: '', //内容
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        classifyId: [
          { required: true, message: '请选择分类', trigger: 'blur' },
        ],
        status: [
          { required: true, message: '请选择发布状态', trigger: 'blur' },
        ],
        contents: [{ required: true, message: '请输入内容', trigger: 'blur' }],
      },
    }
  },
  mounted() {
    this.$bus.$on('showDialogBbsAdd', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showDialogBbsAdd')
  },
  methods: {
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.contents = this.$refs.ueditor.message
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType =
            this.title == '新增' ? 'addBbsTopic' : 'updateBbsTopic'
          await this.$store.dispatch(dispatchType, this.form)
          this.done()
        }
      })
    },
    done() {
      this.dialogVisible = false
      this.$bus.$emit('refreshBbsTopicList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    resetForm(form) {
      this.$refs[form].resetFields()
    },
  },
}
</script>
<style scoped lang="scss">
</style>